﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NReader.Model.RAS;
using NReader.Service;
using Windows.ApplicationModel.Search;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XnReader.WinRT.Controlx;

// “项目页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234233 上提供

namespace XnReader.WinRT.Forms
{
    /// <summary>
    /// 显示项预览集合的页。在“拆分布局应用程序”中，此页
    /// 用于显示及选择可用组之一。
    /// </summary>
    public sealed partial class OnlineStore : XnReader.WinRT.Common.LayoutAwarePage
    {
        private IList<SysWebInfo> _sysWebInfos;

        public OnlineStore()
        {
            this.InitializeComponent(); 
            var visiblesHeader = new CommandInfo
            {
                Theme = "black",
                VisibleLogo = Visibility.Visible,
                VisibleBookcase = Visibility.Visible
            };
            if (header != null)
            {
                header.DataContext = visiblesHeader;
            }
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            try
            {
                //从挂起恢复的操作或网络链接失败
                if ((pageState != null
                         && pageState.ContainsKey("Page")
                         && pageState["Page"].ToString().Equals("OnlineStore"))
                     || await Helper.GlobalUtils.CheckConnectionState() == false)
                {
                    //_groupInfoList = await GlobalService.Instance().GetIndexGroupListFromLocal();
                    //cvs2.Source = _groupInfoList;
                    //pbLoading.IsActive = false;
                    _sysWebInfos = await GlobalService.Instance().GetSysWebInfosFromLocal();
                }
                else //从导航的操作
                {
                    //_groupInfoList = await GlobalService.Instance().GetIndexGroupList();
                    //cvs2.Source = _groupInfoList;
                    //pbLoading.IsActive = false;
                    _sysWebInfos = await GlobalService.Instance().GetSysWebList();
                    await Task.Run(() => SaveIndex());
                }
            }
            catch (Exception ex)
            {
                var md = new MessageDialog("书籍列表加载错误.请检查您的网络链接是否正确");
                md.ShowAsync();
                //throw;
            }
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null)
            {
                var flag = int.Parse(button.Tag.ToString());
                switch (flag)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 101:
                    case 10001:
                        {
                            Frame.Navigate(typeof(BookList), flag);
                        }
                        break;
                    case 10002:
                        {
                            SearchPane.GetForCurrentView().Show();
                        }
                        break;
                }
            }
        }

        private async Task SaveIndex()
        {
            if (_sysWebInfos != null)
            {
                await GlobalService.Instance().SaveSysWebInfos(_sysWebInfos);
            }
            //if (_groupInfoList != null)
            //{
            //    await GlobalService.Instance().SaveIndexGroupList(_groupInfoList);
            //}
        }
    }
}
